package com.example.demo.Controller;

import com.example.demo.Dao.UserMaper;
import com.example.demo.Model.UserEntity;
import com.example.demo.Service.CallBackResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
public class LoginController {
    @Autowired
    private UserMaper userMaper;


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public CallBackResult login(@RequestBody UserEntity userEntity, HttpServletRequest request, HttpSession session){
        UserEntity user=userMaper.findbyAccount(userEntity.getAccont());
        if (user==null){
            return new CallBackResult("用户不存在",new HashMap<>(),500);
        }
        else
        {
//            Object sessionBrowser = session.getAttribute("user");
//            if (sessionBrowser == null) {
//                System.out.println("不存在session，设置user=" + user);
                session.setAttribute("user", user);
//            } else {
//                System.out.println("存在session，user=" + sessionBrowser.toString());
//            }
            return new CallBackResult("登录成功",user,200);
        }
    }
}
